#!/bin/sh
#
# This script is used for adding programs in a chroot jail.
# It builds the chroot jailed system using SuSE packages thorugh 'zypper'.
# The advantage of using packages to build the jail is that it becomes much easier
# to apply patches to the user chroot jail in the future.
# Once the jail is built 'aide' will be run to initialize a database for the
# user. This database can be used (in the future) to determine if any of the files
# in the jail have been change.
#
# Before applying patches, 'aide' is run on the jail to see if anything
# has changed since the last time patches were installed.. After the patches are
# applied 'aide' is again run on the jail to build the database for the next
# itteration of patch applications.
#
# Note:
# With the base system (e.g., from create_chroot) and X11 the root directory is
# about 159 MB. With all packages except MySQL the chroot directory is about 640 MB.
# With just the base system and MySQL the chroot directory is 132 MB.
#
# enchroot is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# enchroot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with enchroot. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (c) 2010 charles@kollar.com
#

#
# Global configuraiton/options are set in this file...
. /usr/local/etc/enchroot.conf

##
## You should not have to change anything below this!
##

CHROOT_USERNAME=$1

JAILPATH=$HOME_CHROOT/$CHROOT_USERNAME

USERAIDEDIR=$AIDEDIR/$CHROOT_USERNAME

# don't really need to do this unless the user has added something below...
echo ""
echo "*****Checking chroot jail '$JAILPATH' for changes..."
echo ""
if [ -f $USERAIDEDIR/aide.db.new ] ; then
  cp $USERAIDEDIR/aide.db.new $USERAIDEDIR/aide.db
fi
aide --check --config=$USERAIDEDIR/aide.conf


# NOTE: The error message below indicates that libXext has support for generic events, but the server does not yet (e.g., ignore it).
# Xlib: extension "Generic Event Extension" missing on display ":0.0".
INSTALLX11="no"
echo ""
echo -n "Install X11? "
read -p "[yes/no] (no): " INSTALLX11
if [ "$INSTALLX11" == "yes" ]; then
  echo "Installing X11..."
  zypper --root $JAILPATH install --no-recommends --auto-agree-with-licenses --type package xorg-x11 xorg-x11-fonts-core
fi

INSTALLJAVA="no"
echo ""
echo -n "Install the Java runtime environment? "
read -p "[yes/no] (no): " INSTALLJAVA
if [ "$INSTALLJAVA" == "yes" ]; then
  echo "Installing the Java runtime environment..."
  zypper --root $JAILPATH install --no-recommends --auto-agree-with-licenses --type package java-1_6_0-sun
fi

INSTALLMC="no"
echo ""
echo -n "Install MySQL Client and Tools? "
read -p "[yes/no] (no): " INSTALLMC
if [ "$INSTALLMC" == "yes" ]; then
  echo "Installing MySQL Client and Tools..."
  # NOTE: we also need to have installed X11... should make these into functions.
  # This is the directory that we are putting "just packages" in for a particular user...
  if ! [ -d ${JAILPATH}/usr/local/share/rpm ] ; then
    echo "Creating directory for local repository ${JAILPATH}/usr/local/share/rpm..."
    mkdir -p ${JAILPATH}/usr/local/share/rpm
    echo "Creating repository repo-rpm..."
    zypper --root $JAILPATH addrepo --name "Up-to-date packages" ${JAILPATH}/usr/local/share/rpm repo-rpm
    echo "Listing repositories currently in the chroot jail..."
    zypper --root $JAILPATH repos --uri
  fi
  # This is the package that we need. Only doing this because the 11.2 distribution of SuSE has a two year old
  # version of mysql-workbench?!
  if ! [ -f ${JAILPATH}/usr/local/share/rpm/mysql-workbench-gpl-5.2.25-1suse112.x86_64.rpm ] ; then
    (cd ${JAILPATH}/usr/local/share/rpm; wget "http://dev.mysql.com/get/Downloads/MySQLGUITools/mysql-workbench-gpl-5.2.25-1suse112.x86_64.rpm/from/ftp://mirror.anl.gov/pub/mysql/")
  fi
  zypper --root $JAILPATH install --no-recommends --auto-agree-with-licenses --type package gtk2 mysql-client mysql-tools mysql-gui-tools mysql-workbench-gpl # mysql-workbench
  # Generally connections to the mysql server on a local machine go through a 'mysql.sock' file located
  # at '/var/run/mysql'. So you will need to remember making a hard link between the location in the
  # server user's ara and this client's area...
  if ! [ -d ${JAILPATH}/var/run/mysql ] ; then
    echo "Creating ${JAILPATH}/var/run/mysql..."
    mkdir -p ${JAILPATH}/var/run/mysql
  fi
  echo
  echo "**** Remember to make a 'hard link' from the server of choice mysql.sock file to thie client's mysql.sock file"
  echo "**** ln /home/chroot/mysqld/var/run/mysql/mysql.sock  /home/chroot/mysqluser/var/run/mysql/mysql.sock"
  echo
fi

INSTALLC="no"
echo ""
echo -n "Install C/C++/fortran? "
read -p "[yes/no] (no): " INSTALLC
if [ "$INSTALLC" == "yes" ]; then
  echo "Installing C/C++/fortran..."
  zypper --root $JAILPATH install --no-recommends --auto-agree-with-licenses --type package gcc gcc-c++ gcc-fortran gdb make autoconf
fi

INSTALLPYTHON="no"
echo ""
echo -n "Install Python? "
read -p "[yes/no] (no): " INSTALLPYTHON
if [ "$INSTALLPYTHON" == "yes" ]; then
  echo "Installing python..."
  zypper --root $JAILPATH install --no-recommends --auto-agree-with-licenses --type package python-devel python-mysql python-satsolver python-xlib
fi

# Lindar Algebra PACKage (LAPACK)
# http://www.netlib.org/lapack/
#
# Message Passing Interface (MPI) 
# http://www.open-mpi.org/
# http://www.mcs.anl.gov/research/projects/mpich2/
#
# Basic Linear Algebra Subprograms (BLAS)
# http://www.netlib.org/blas/
#
# Suite of Nonlinear and Differential/Algebraic Equation Solvers (SUNDIALS). IncludesCVODE, VODPK, NKSOL, DASPK
# Lawrence Livermore National Laboratory
# https://computation.llnl.gov/casc/sundials/main.html
# NOT AVAILABLE AS A SUSE PACKAGE!!

INSTALLLAPACK="no"
echo ""
echo -n "Install lapack/openmpi/mpich/blas? "
read -p "[yes/no] (no): " INSTALLLAPACK
if [ "$INSTALLLAPACK" == "yes" ]; then
  echo "Installing lapack/openmpi/mpich/blas..."
  # NOTE: Also need c/c++ to use these packages
  zypper --root $JAILPATH install --no-recommends --auto-agree-with-licenses --type package lapack openmpi-devel mpich-devel blas gcc gcc-c++ gdb make autoconf
fi

INSTALLSUNDIALS="no"
echo ""
# It's not available as an 'rpm'...
# NOTE: Since this is being built and installed in /usr/local, we don't need the encrypted home directory...
echo -n "Build and Install sundials? "
read -p "[yes/no] (no): " INSTALLSUNDIALS
if [ "$INSTALLSUNDIALS" == "yes" ]; then
  SUNDIALS_VERS=2.4.0
  echo "Build and Installing sundials..."
  # NOTE: Also need c/c++ to use these packages...
  zypper --root $JAILPATH install --no-recommends --auto-agree-with-licenses --type package lapack blas gcc gcc-c++ gcc-fortran make autoconf
  # Do this in the user's area
  mkdir -p $JAILPATH/usr/local/src
  # Get the tarball for sundials, and unpack it into working directory...
  (cd $JAILPATH/usr/local/src; wget https://computation.llnl.gov/casc/sundials/download/code/sundials-${SUNDIALS_VERS}.tar.gz; tar zxf sundials-${SUNDIALS_VERS}.tar.gz; )
  # Configure, make and install to /usr/local...
  # NOTE: F77 & package gcc-fortran is needed for creating the lapack libraries...
  chroot $JAILPATH /bin/su --command="cd /usr/local/src/sundials-${SUNDIALS_VERS}; export F77=gfortran; ./configure --with-lapack=/usr/lib64 --with-blas=/usr/lib64; make; make install; " root
  # Cleanup...
  # Keep the downloaded tar.gz file, but get rid of the files...
  echo "Deleting source directory..."
  rm -rf $JAILPATH/usr/local/src/sundials-${SUNDIALS_VERS}
fi

INSTALLR="no"
echo ""
echo -n "Install R? "
read -p "[yes/no] (no): " INSTALLR
if [ "$INSTALLR" == "yes" ]; then
# http://cran.r-project.org/bin/linux/suse/README.html
  echo
  echo "Installing repository containing R..."
   zypper --root $JAILPATH addrepo --refresh http://download.opensuse.org/repositories/devel\:languages\:R\:patched/openSUSE_$SUSE_VERSION R-patched
  echo
  echo "Listing repositories in the chroot jail..."
  zypper --root $JAILPATH repos --uri
  echo
  echo "Installing R..."
  # http://cran.r-project.org/bin/linux/suse/README.html
  zypper --root $JAILPATH install --no-recommends --auto-agree-with-licenses --type package R-patched R-patched-devel less
fi

#echo ""
#echo "Removing the suid/sgid bit for the following files..."
#find ${JAILPATH} \( -perm -004000 -o -perm -002000 \) -type f -exec ls -l {} \; -exec /bin/chmod -s {} \;
echo "The suid/sgid bit is set for the following files..."
find ${JAILPATH} \( -perm -004000 -o -perm -002000 \) -type f -exec ls -l {} \;


# don't really need to do this unless the user has added something above...
echo ""
echo "Creating user's aide database..."
aide --init --config=$AIDEDIR/$CHROOT_USERNAME/aide.conf
